<?php
	function get_category_list()
	{
		$sql = 'select c.*,count(s.cid) as has_children from category as c left join category as s on s.pid=c.cid group by c.cid order by c.pid';
		$db = db::getInstance();
		$cat = $db->query($sql)->result_array();
		
		return $cat;
	}
	
	function cat_options($spec_cat_id, $arr)
	{   
	    //$last_cat_id 当前分类的父ID
	    //$level 当前层级.
	    $level = $last_cat_id = 0;
	    //$cat_id_array 记录父分类ID
	    //$level_array 记录什么呢?
	    $options = $cat_id_array = $level_array = array();  
	  	//历遍整个分类数据库
	    while (!empty($arr))
	    {
	        foreach ($arr AS $key => $value)
	        {
	            $cat_id = $value['cid'];
	            if ($level == 0 && $last_cat_id == 0)
	            {
	                if ($value['pid'] > 0)
	                {
	                    break;
	                }
	
	                $options[$cat_id]          = $value;
	                $options[$cat_id]['level'] = $level;
	                $options[$cat_id]['id']    = $cat_id;
	                $options[$cat_id]['name']  = $value['cat_name'];
	                unset($arr[$key]);
	
	                if ($value['has_children'] == 0)
	                {
	                    continue;
	                }
	                $last_cat_id  = $cat_id;
	                $cat_id_array = array($cat_id);
	                $level_array[$last_cat_id] = ++$level;
	                continue;
	            }
	
	            if ($value['pid'] == $last_cat_id)
	            {
	                $options[$cat_id]          = $value;
	                $options[$cat_id]['level'] = $level;
	                $options[$cat_id]['id']    = $cat_id;
	                $options[$cat_id]['name']  = $value['cat_name'];
	                unset($arr[$key]);
	
	                if ($value['has_children'] > 0)
	                {
	                    if (end($cat_id_array) != $last_cat_id)
	                    {
	                        $cat_id_array[] = $last_cat_id;
	                    }
	                    $last_cat_id    = $cat_id;
	                    $cat_id_array[] = $cat_id;
	                    $level_array[$last_cat_id] = ++$level;
	                }
	            }
	            elseif ($value['pid'] > $last_cat_id)
	            {
	                break;
	            }
	        }
	
	        $count = count($cat_id_array);
	        if ($count > 1)
	        {
	            $last_cat_id = array_pop($cat_id_array);
	        }
	        elseif ($count == 1)
	        {
	            if ($last_cat_id != end($cat_id_array))
	            {
	                $last_cat_id = end($cat_id_array);
	            }
	            else
	            {
	                $level = 0;
	                $last_cat_id = 0;
	                $cat_id_array = array();
	                continue;
	            }
	        }
	
	        if ($last_cat_id && isset($level_array[$last_cat_id]))
	        {
	            $level = $level_array[$last_cat_id];
	        }
	        else
	        {
	            $level = 0;
	        }
	    }
	       
	    return $options;
	   
	}

	function load_lib($mod_name)
	{
		$file = ROOTPATH.'lib/'.$mod_name.'.php';
		if(file_exists($file)){
			require $file;
		}else{
			echo 'file: '.$mod_name . 'not found ';
			exit();
		}
	}
	
	function load_plugin($plug_name)
	{
		$file = ROOTPATH.'plugins/'.$plug_name.'.php';
		if(file_exists($file)){
			require $file;
		}else{
			echo 'file: '.$plug_name . 'not found ';
			exit();
		}
	}
?>